<?php
/**
 * +--------------------------------------------------------+
 * @Created by PhpStorm.
 * @Category SettingConfigController.php
 * @Depiction: 开发配置-配置分类的详细配置
 * Author: hwy
 * Date: 2020/6/29  15:48
 * +--------------------------------------------------------+
 */

namespace App\Http\Controllers\Admin;


use App\Http\Controllers\Controller;
use App\Services\SettingConfigService;
use App\Services\SettingConfigTabService;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\View\Factory;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\View\View;

class SettingConfigController extends Controller
{
    protected $_model = 'setting_config';

    protected $settingConfigService;

    protected $settingConfigTabService;

    public function __construct()
    {
        $this->settingConfigService    = SettingConfigService::getInstance();
        $this->settingConfigTabService = SettingConfigTabService::getInstance();
    }

    /**
     * @param Request $request
     * @return Application|Factory|View
     * @Depiction:
     * @Author: hwy
     * @Date: 2021/3/8  17:35
     */
    public function index(Request $request)
    {
        $compact = compact('request');

        return view('admin.' . $this->_model . '.index', $compact);
    }

    /**
     * @param Request $request
     * @return JsonResponse
     * @Depiction 列表数据
     * @Author hwy
     * @Date  2020/10/10  19:21
     */
    public function getList(Request $request): JsonResponse
    {
        $responseData = $this->settingConfigService->getList($request);

        return response()->json($responseData);
    }

    /**
     * @param Request $request
     * @return Factory|View
     * @Depiction 添加详细配置字段
     * @Author hwy
     * @Date  2020/10/10  19:21
     */
    public function create(Request $request)
    {
        empty($request->input_type) ? $request->input_type = 'text' : $request->input_type;
        $setting_config_tab = $this->settingConfigTabService->getAll()->pluck('title', 'id');
        $input_type         = config('other.input_type');
        $view               = 'admin.' . $this->_model . '.create_' . $request->input_type;

        switch ($request->input_type) {
            case 'text':
                $text_box_type = config('other.text_box_type');
                $compact       = compact('request', 'setting_config_tab', 'text_box_type', 'input_type');
                break;
            case 'upload':
                $upload_box_type = config('other.upload_box_type');
                $compact         = compact('request', 'setting_config_tab', 'upload_box_type', 'input_type');
                break;
            default:
                $compact = compact('request', 'setting_config_tab', 'input_type');
                break;
        }

        return view($view, $compact);
    }

    /**
     * @param Request $request
     * @return JsonResponse
     * @Depiction
     * @Author hwy
     * @Date 2021/3/8  17:50
     */
    public function store(Request $request): JsonResponse
    {
        $responseData = $this->settingConfigService->addData($request);

        return response()->json($responseData);

    }

    /**
     * @param Request $request
     * @param $id
     * @return Factory|View
     * @Depiction
     * @Author hwy
     * @Date  2020/10/10  19:26
     */
    public function edit(Request $request, $id)
    {
        $setting_config_tab  = $this->settingConfigTabService->getAll()->pluck('title', 'id');
        $data                = $this->settingConfigService->getOne($id);
        $request->input_type = $data->type;
        $view                = 'admin.setting_config.edit_' . $request->input_type;

        switch ($request->input_type) {
            case 'text':
                $text_box_type = config('other.text_box_type');
                $compact       = compact('request', 'setting_config_tab', 'text_box_type', 'data');
                break;
            case 'upload':
                $upload_box_type = config('other.upload_box_type');
                $compact         = compact('request', 'setting_config_tab', 'upload_box_type', 'data');
                break;
            default:
                $compact = compact('request', 'setting_config_tab', 'data');
                break;
        }

        return view($view, $compact);

    }

    /**
     * @param Request $request
     * @param $id
     * @return JsonResponse
     * @Depiction
     * @Author hwy
     * @Date  2020/10/10  19:26
     */
    public function update(Request $request, $id): JsonResponse
    {
        $responseData = $this->settingConfigService->editData($request, $id);

        return response()->json($responseData);
    }

    /**
     * @param $id
     * @return JsonResponse
     * @Depiction
     * @Author hwy
     * @Date  2020/10/10  19:26
     */
    public function destroy($id): JsonResponse
    {
        $responseData = $this->settingConfigService->delData($id);

        return response()->json($responseData);
    }

    /**
     * @param $id
     * @return JsonResponse
     * @Depiction
     * @Author hwy
     * @Date  2020/10/10  19:27
     */
    public function setStatus($id): JsonResponse
    {
        $responseData = $this->settingConfigService->setStatus($id);

        return response()->json($responseData);
    }

    /**
     * @param Request $request
     * @return Factory|View
     * @Depiction
     * @Author hwy
     * @Date  2020/10/10  19:27
     */
    public function selectType(Request $request)
    {
        $input_type = config('other.input_type');
        $compact    = compact('request', 'input_type');

        return view('admin.setting_config._select_type', $compact);
    }

}
